import { _decorator, Component, Node, Input, EventTouch, geometry, Camera, PhysicsSystem, Vec3 } from 'cc';
import { CollisionType, GameState } from '../framework/Enum';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;

@ccclass('UiMain')
export class UiMain extends Component {
    // 移动速度参数
    @property
    public planeSpeed = 1
    // 飞机节点
    @property(Node)
    public playerPlane: Node = null
    // 游戏管理模块引用
    @property(GameManager)
    public gameManager: GameManager = null
    // 游戏开始UI
    @property(Node)
    public gameStart: Node = null
    // 游戏中UI
    @property(Node)
    public gameing: Node = null
    // 游戏结束UI
    @property(Node)
    public gameOver: Node = null
    // 主相机
    @property(Camera)
    public mainCamera: Camera = null
    // 触摸点
    private _touchPoint: Vec3 = null

    start() {
        // 全局触摸移动事件
        this.node.on(Input.EventType.TOUCH_MOVE, this._touchMove, this)
        // 全局触摸开始事件
        this.node.on(Input.EventType.TOUCH_START, this._touchStart, this)
        // 全局触摸结束事件
        this.node.on(Input.EventType.TOUCH_END, this._touchEnd, this)
        // 显示游戏开始UI
        this.gameStart.active = true
    }

    update(deltaTime: number) {
        if (!this.gameManager.isInGame() || !this._touchPoint) return
        // 当前飞机位置
        let pos = this.playerPlane.position
        // 移动距离
        const distanceX = this._planeMove(pos.x, this._touchPoint.x, deltaTime)
        const distanceZ = this._planeMove(pos.z, this._touchPoint.z, deltaTime)
        this.playerPlane.setPosition(pos.x + distanceX, pos.y, pos.z + distanceZ)
    }

    private _planeMove(planePos: number, touchPos: number, deltaTime) {
        let distance = touchPos - planePos
        const sign = distance >= 0 ? 1 : -1

        const maxDistance = this.planeSpeed * deltaTime * Math.abs(distance)

        return Math.abs(distance) > maxDistance ? maxDistance * sign : distance
    }

    // 触摸开始事件
    private _touchStart(event: EventTouch) {
        // 游戏已经开始，则触发触摸移动事件
        if(this.gameManager.isInGame()) this._touchMove(event)
        if (!this.gameManager.isGameStart()) return

        // 开始游戏
        this.gameManager.setGameState(GameState.INGAME)
        this.gameStart.active = false
        this.gameing.active = true
    }
    // 触摸移动事件
    private _touchMove(event: EventTouch) {
        if (!this.gameManager.isInGame()) return

        let ray = new geometry.Ray()
        this.mainCamera.getComponent(Camera).screenPointToRay(event.getLocationX(), event.getLocationY(), ray);
        if (!PhysicsSystem.instance.raycast(ray, CollisionType.BACKGROUND)) return
        // 获取到触摸点到场景背景的投影点
        this._touchPoint = PhysicsSystem.instance.raycastResults[0].hitPoint;
    }
    // 触摸结束事件
    private _touchEnd(event: EventTouch) {
        if (!this.gameManager.isInGame()) return
        this._touchPoint = null
        // this.gameManager.setIsShooting(false)
    }

    // 重新开始事件
    public reStart() {
        this._touchPoint = null
        this.gameManager.setGameState(GameState.INGAME)
        this.gameManager.audioMain.play('button')
        this.gameOver.active = false
        this.gameing.active = true
    }

    // 返回主页事件
    public returnMain() {
        this._touchPoint = null
        this.gameManager.setGameState(GameState.START)
        this.gameManager.audioMain.play('button')
        this.gameOver.active = false
        this.gameStart.active = true
    }
}

